package com.salton123.ui.base

import android.app.Activity
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import com.gyf.immersionbar.ImmersionBar
import com.hjq.bar.OnTitleBarListener
import com.hjq.bar.TitleBar
import com.salton123.arch.view.IMultiStatusView
import com.salton123.arch.view.ITitleView
import com.salton123.feature.multistatus.MultiStatusFeature
import com.salton123.util.ScheduledTask

/**
 * User: newSalton@outlook.com
 * Date: 2019/12/12 15:59
 * ModifyTime: 15:59
 * Description:
 */
abstract class BaseFragment : LifeDelegateFragment(), IMultiStatusView, ITitleView {
    private var mTitleBar: TitleBar? = null
    private var mMultiStatusFeature: MultiStatusFeature? = null
    private var mImmersionBar: ImmersionBar? = null

    //多状态,方便重写
    open fun fetchMultiStatusFeature(): MultiStatusFeature? {
        if (mMultiStatusFeature == null) {
            mMultiStatusFeature = MultiStatusFeature(this)
        }
        return mMultiStatusFeature
    }

    open fun isOpenMultiStatus(): Boolean {
        return true
    }

    //沉浸式,方便重写
    open fun getImmersionBar(): ImmersionBar? {
        if (mImmersionBar == null) {
            if (self() is Activity) {
                mImmersionBar = ImmersionBar.with(self() as Activity)
            } else if (self() is Fragment) {
                mImmersionBar = ImmersionBar.with(self() as Fragment)
            }
        }
        return mImmersionBar
    }

    open fun defaultImmersionStyle() {
        getImmersionBar()?.statusBarDarkFont(true)?.init()
    }

    open fun isOpenImmersionBar(): Boolean {
        if (self() is Activity) {
            return true
        } else if (self() is Fragment) {
            return false
        }
        return false
    }

    //通用标题栏,方便重写
    open fun fetchTitleBar(): TitleBar? {
        if (self() is BaseActivity) {
            if (mTitleBar == null) {
                mTitleBar = TitleBar(activity())
                mTitleBar?.setOnTitleBarListener(mOnTitleBarListener)
                mTitleBar?.addStatusBar()
            }
        }
        return mTitleBar
    }

    open fun isOpenTitleBar(): Boolean {
        if (self() is Activity) {
            return true
        } else if (self() is Fragment) {
            return false
        }
        return false
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        if (isOpenTitleBar()) {
            mTitleBar = fetchTitleBar()
        }
        if (isOpenMultiStatus()) {
            mMultiStatusFeature = fetchMultiStatusFeature()
            mMultiStatusFeature?.apply {
                addFeature(this)
            }
        }
        if (isOpenImmersionBar()) {
            defaultImmersionStyle()
        }
        super.onCreate(savedInstanceState)
    }


    override fun initListener() {
        asynTitleBar(mTitleBar)
        showSuccessView()
        super.initListener()
    }

    override fun showInitLoadView() {
        mMultiStatusFeature?.showInitLoadView()
    }

    override fun showNoDataView() {
        mMultiStatusFeature?.showNoDataView()
    }

    override fun showTransLoadingView() {
        mMultiStatusFeature?.showTransLoadingView()
    }

    override fun showNetWorkErrView() {
        mMultiStatusFeature?.showNetWorkErrView()
    }

    override fun showSuccessView() {
        ScheduledTask.getInstance().postDelayed({
            try {
                mMultiStatusFeature?.showSuccessView()
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }, 500)
    }

    override fun setLeftIcon(resId: Int): ITitleView {
        mTitleBar?.setLeftIcon(resId)
        return this
    }

    override fun setLeftIcon(drawable: Drawable): ITitleView {
        mTitleBar?.setLeftIcon(drawable)
        return this
    }

    override fun setLeftText(leftText: CharSequence): ITitleView {
        mTitleBar?.setLeftTitle(leftText)
        return this
    }

    override fun setLeftText(stringId: Int): ITitleView {
        mTitleBar?.setLeftTitle(stringId)
        return this
    }

    override fun setLeftColor(color: Int): ITitleView {
        mTitleBar?.setLeftColor(color)
        return this
    }

    override fun setLeftBackground(id: Int): ITitleView {
        mTitleBar?.setLeftBackground(id)
        return this
    }

    override fun setLeftBackground(drawable: Drawable): ITitleView {
        mTitleBar?.setLeftBackground(drawable)
        return this
    }

    override fun setLeftSize(unit: Int, size: Float): ITitleView {
        mTitleBar?.setLeftSize(unit, size)
        return this
    }

    override fun setRightIcon(resId: Int): ITitleView {
        mTitleBar?.setRightIcon(resId)
        return this
    }

    override fun setRightIcon(drawable: Drawable): ITitleView {
        mTitleBar?.setRightIcon(drawable)
        return this
    }

    override fun setRightText(rightText: CharSequence): ITitleView {
        mTitleBar?.setRightTitle(rightText)
        return this
    }

    override fun setRightText(stringId: Int): ITitleView {
        mTitleBar?.setRightTitle(stringId)
        return this
    }

    override fun setRightColor(color: Int): ITitleView {
        mTitleBar?.setRightColor(color)
        return this
    }

    override fun setRightBackground(id: Int): ITitleView {
        mTitleBar?.setRightBackground(id)
        return this
    }

    override fun setRightBackground(drawable: Drawable): ITitleView {
        mTitleBar?.setRightBackground(drawable)
        return this
    }

    override fun setRightSize(unit: Int, size: Float): ITitleView {
        mTitleBar?.setRightSize(unit, size)
        return this
    }

    override fun setTitleTextColor(color: Int): ITitleView {
        mTitleBar?.setTitleColor(color)
        return this
    }

    override fun setTitleText(titleText: CharSequence): ITitleView {
        mTitleBar?.setTitle(titleText)
        return this
    }

    override fun setTitleText(stringId: Int): ITitleView {
        mTitleBar?.setTitle(stringId)
        return this
    }

    override fun setTitleSize(unit: Int, size: Float): ITitleView {
        mTitleBar?.setTitleSize(unit, size)
        return this
    }

    override fun setTitleGravity(gravity: Int): ITitleView {
        mTitleBar?.setTitleGravity(gravity)
        return this
    }

    override fun setLineVisible(visible: Boolean): ITitleView {
        mTitleBar?.setLineVisible(visible)
        return this
    }

    override fun setLineColor(color: Int): ITitleView {
        mTitleBar?.setLineColor(color)
        return this
    }

    override fun setLineDrawable(drawable: Drawable): ITitleView {
        mTitleBar?.setLineDrawable(drawable)
        return this
    }

    override fun setLineSize(px: Int): ITitleView {
        mTitleBar?.setLineSize(px)
        return this
    }

    override fun setDrawablePadding(px: Int): ITitleView {
        mTitleBar?.setDrawablePadding(px)
        return this
    }

    override fun setChildPadding(px: Int): ITitleView {
        mTitleBar?.setChildPadding(px)
        return this
    }

    override fun setOnTitleBarListener(l: OnTitleBarListener): ITitleView {
        mTitleBar?.setOnTitleBarListener(l)
        return this
    }

    private val mOnTitleBarListener = object : OnTitleBarListener {
        override fun onLeftClick(v: View?) {
            onLeftClick()
        }

        override fun onRightClick(v: View?) {
            onRightClick()
        }

        override fun onTitleClick(v: View?) {
            onTitleClick()
        }
    }

    open fun onTitleClick() {}
    open fun onRightClick() {}
    open fun onLeftClick() {
        when {
            self() is BaseActivity -> activity().finish()
            self() is BaseFragment -> activity().onBackPressed()
            self() is BaseDialogFragment -> {
                val baseDialogFragment = self() as BaseDialogFragment
                baseDialogFragment.dismissAllowingStateLoss()
            }
        }
    }
}
